package cn.kgc.base.controller;

import cn.kgc.base.entity.SysDict;
import cn.kgc.commons.constant.SysDictItemStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.kgc.base.entity.SysDictItem;
import cn.kgc.base.dto.SysDictItemAddDTO;
import cn.kgc.base.dto.SysDictItemUpdateDTO;
import cn.kgc.base.service.SysDictItemService;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.kgc.base.entity.SysDictItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * (SysDictItem)表控制层
 *
 * @author 石浩杰
 * @since 2022-11-30 14:50:46
 */
@RestController
@RequestMapping("/sys/dict/item")
@Api(tags = "字典项管理")
@Validated
@ConditionalOnClass(ResponseBodyAdvice.class)
public class SysDictItemController {
    /**
     * 服务对象
     */
    @Resource
    private SysDictItemService sysDictItemService;

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询字典项数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "dictId", value = "字典Id", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "itemText", value = "字典项名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "status", value = "字典项状态", paramType = "query", dataType = "int")
    })
    @CrossOrigin
    public IPage<SysDictItem> selectAll(@RequestParam(defaultValue = "1") int current,
                                        @RequestParam(defaultValue = "5") int pageSize,
                                        @RequestParam(defaultValue = "") Long dictId,
                                        @RequestParam(defaultValue = "") String itemText,
                                        @RequestParam(defaultValue = "2") int status) {
        IPage<SysDictItem> page = new Page(current, pageSize);
        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictItem::getDictId, dictId);
        queryWrapper.like(StringUtils.isNotBlank(itemText), SysDictItem::getItemText, itemText);
        queryWrapper.eq(status != SysDictItemStatus.NOT_CHOOSE.getStatus(), SysDictItem::getStatus, status);
        queryWrapper.orderByAsc(SysDictItem::getSortOrder);
        return sysDictItemService.page(page,queryWrapper);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public SysDictItem selectOne(@PathVariable Serializable id) {
        return this.sysDictItemService.getById(id);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param dictId 主键
     * @return 单条数据
     */
    @GetMapping("/dict/{dictId}")
    @CrossOrigin
    @ApiOperation(value = "通过字典id查询对应字典项", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "dictId", value = "字典Id", paramType = "path", dataType = "int")
    public List<SysDictItem> selectOne(@PathVariable String dictId) {
        LambdaQueryWrapper<SysDictItem> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictItem::getDictId,dictId);
        return this.sysDictItemService.list(lambdaQueryWrapper);
    }

    /**
     * 新增数据
     *
     * @param sysDictItemDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean insert(@RequestBody SysDictItemAddDTO sysDictItemDTO) {
        SysDictItem sysDictItem = new SysDictItem();
        BeanUtils.copyProperties(sysDictItemDTO, sysDictItem);
        sysDictItem.setStatus(SysDictItemStatus.ENABLE.getStatus());
        String createBy="scott";
        sysDictItem.setCreateBy(createBy); //todo 添加新增字典项的人的信息
        return this.sysDictItemService.save(sysDictItem);
    }


    @PostMapping("/{id}}")
    @ApiOperation(value = "启用禁用某个数据字典项", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @ApiImplicitParam(name="id",value = "主键",paramType = "path",dataType = "long")
    @CrossOrigin
    public boolean updateSysDictItemStatus(@PathVariable Long id){
        //查询原有状态
        SysDictItem sysDictItem = sysDictItemService.getById(id);
        int status=sysDictItem.getStatus();
        //判读原来是否是启用状态，如果是启用状态，就禁用，如果是禁用状态，就启用
        status=status==SysDictItemStatus.ENABLE.getStatus()?
                SysDictItemStatus.DISABLE.getStatus():SysDictItemStatus.ENABLE.getStatus();
        sysDictItem.setStatus(status);
        //更新状态
        return sysDictItemService.updateById(sysDictItem);
    }

    /**
     * 修改数据
     *
     * @param sysDictItemDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean update(@RequestBody SysDictItemUpdateDTO sysDictItemDTO) {
        SysDictItem sysDictItem = this.sysDictItemService.getById(sysDictItemDTO.getId());
        BeanUtils.copyProperties(sysDictItemDTO, sysDictItem);
        String updateBy = "scott";
        sysDictItem.setUpdateBy(updateBy); //todo 添加修改字典项的人的信息
        return this.sysDictItemService.updateById(sysDictItem);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.sysDictItemService.removeById(id);
    }
}
